#include "config.h"
#include "unp.h"

int main(int argc, char **argv)
{
    
    int rtsock, pgrepsock, pgreqsock,i;
    const int on = 1;
    int isSource=0;
    struct hostent *hptr;
    
    //Validate all the input vm names.
    if(argc > 1)
    {
        //Source Node
        isSource = 1;
     
        char vm_name[MAXLINE];
        
        for(i=1; i <= argc-1; i++)
        {
            if(sscanf(argv[i],"vm%2[0-9]",vm_name) == 1) 
            {
                int node_no = atoi(vm_name);
                if(!(node_no <= 10)) 
                {			
                    printf("\nInvalid command line arguments!\n");
                    fflush(stdout);	
                    exit(0);
                }
            }
            else
            {
                    printf("\nInvalid command line arguments\n");
                    fflush(stdout);	
                    exit(0);
            }
        }
    } //end of validation if


    //Resolve all vm names
    for(i=1; i <= argc-1; i++)
    {    
        if((hptr = gethostbyname(argv[i])) == NULL)
	{
            printf("\nError resolving vm hostname. Please try again\n\n");
            fflush(stdout);
            herror(NULL);
            exit(0);
	}
		
        // Destination IP address
        char buf[INET_ADDRSTRLEN];

        Inet_ntop(hptr->h_addrtype,hptr->h_addr_list[0],buf,sizeof(buf));
        printf("\nIP address is:%s\n",buf);
        fflush(stdout);	
		
    }
    
        
    //Create Routing IP RAW Socket
    rtsock=Socket(AF_INET, SOCK_RAW, RTProtocolNo);
    
    //Set Header Inclusive for Routing IP Socket
    if(setsockopt(rtsock,IPPROTO_IP,IP_HDRINCL,&on,sizeof(on))<0)
    {
        printf("\nError: Unable to set socket option header inclusive.\n");
        fflush(stdout);
        exit(0);
    }
        
    //Create IP Raw socket for Ping ICMP Echo Replies.
    pgrepsock=Socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);

    //Create Packet Socket for Ping ICMP Echo Request.
    pgreqsock = Socket(PF_PACKET, SOCK_RAW, htons(ETH_P_IP));
    
    printf("Reached here");
    fflush(stdout);
    
    
    //NEXT STEPS
    //1. Check that source node is not part of the tour.
    //2. Make utiltiy function for retreiving IP from name.
    //3. Create Data structure for storing IP.
    //4. IP Payload structure.d
    
    
}